-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Include post ID in custom metadata #2511
base: develop
Are you sure you want to change the base?
Conversation
📝 Walkthrough📝 WalkthroughWalkthroughThe recent changes introduce a new method Changes
Sequence Diagram(s) (Beta)sequenceDiagram
participant Client
participant MetadataBuilder
participant WP_Post
Client->>MetadataBuilder: build_post_id(WP_Post)
MetadataBuilder->>WP_Post: Access post data
WP_Post-->>MetadataBuilder: Return post ID
MetadataBuilder-->>Client: Return custom metadata with post ID
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
Documentation and Community
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
Review details
Configuration used: .coderabbit.yaml
Review profile: CHILL
Files selected for processing (2)
- src/Metadata/class-metadata-builder.php (1 hunks)
- src/Metadata/class-post-builder.php (1 hunks)
Additional context used
Path-based instructions (2)
src/Metadata/class-post-builder.php (1)
Pattern
**/*.{html,php}
: "Perform a detailed review of the provided code with following key aspects in mind:
- Review the HTML and PHP code to ensure it is well-structured and adheres to best practices.
- Ensure the code follows WordPress coding standards and is well-documented.
- Confirm the code is secure and free from vulnerabilities.
- Optimize the code for performance, removing any unnecessary elements.
- Validate comments for accuracy, currency, and adherence to WordPress coding standards.
- Ensure each line comment concludes with a period.
- Verify code compatibility with the latest version of WordPress, avoiding deprecated functions or features."
src/Metadata/class-metadata-builder.php (1)
Pattern
**/*.{html,php}
: "Perform a detailed review of the provided code with following key aspects in mind:
- Review the HTML and PHP code to ensure it is well-structured and adheres to best practices.
- Ensure the code follows WordPress coding standards and is well-documented.
- Confirm the code is secure and free from vulnerabilities.
- Optimize the code for performance, removing any unnecessary elements.
- Validate comments for accuracy, currency, and adherence to WordPress coding standards.
- Ensure each line comment concludes with a period.
- Verify code compatibility with the latest version of WordPress, avoiding deprecated functions or features."
Additional comments not posted (2)
src/Metadata/class-post-builder.php (1)
63-63
: The integration ofbuild_post_id
intoget_metadata
looks good.Verification successful
The
build_post_id
method is defined inclass-metadata-builder.php
and called inclass-post-builder.php
. Let's verify the implementation ofbuild_post_id
to ensure it aligns with the intended functionality.
The
ast-grep
tool does not support PHP, which caused an error. Let's userg
to search for thebuild_post_id
method definition and extract its implementation.
The
build_post_id
method populates thecustom_metadata
field with the post ID. Let's verify that thecustom_metadata
field is correctly integrated and used in the metadata.
The
custom_metadata
field is used inclass-metadata.php
andclass-metadata-renderer.php
. Let's verify the usage inclass-metadata-renderer.php
to ensure proper handling and rendering.
The
custom_metadata
field is correctly handled inclass-metadata-renderer.php
, where it is checked for existence and then rendered as a meta tag. This confirms that the integration of thebuild_post_id
method and thecustom_metadata
field is properly implemented and does not conflict with other metadata fields.
class-metadata-renderer.php
:
- Properly checks for the existence of
custom_metadata
.- Renders
custom_metadata
as a meta tag.Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify that the `build_post_id` method integrates well with other metadata components. # Test: Search for the method usage in related files. Expect: Proper integration without conflicts. rg --type php $'build_post_id'Length of output: 203
Script:
#!/bin/bash # Description: Verify the implementation of the `build_post_id` method in `class-metadata-builder.php`. # Extract the implementation of the `build_post_id` method. ast-grep --lang php --pattern $'protected function build_post_id( WP_Post $post ): void { $$$ }' --jsonLength of output: 213
Script:
#!/bin/bash # Description: Verify the implementation of the `build_post_id` method in `class-metadata-builder.php`. # Search for the `build_post_id` method definition and display its implementation. rg --type php --context 10 'protected function build_post_id' src/Metadata/class-metadata-builder.phpLength of output: 538
Script:
#!/bin/bash # Description: Verify the usage of the `custom_metadata` field in the codebase. # Search for the usage of the `custom_metadata` field in the codebase. rg --type php 'custom_metadata'Length of output: 402
Script:
#!/bin/bash # Description: Verify the implementation of `custom_metadata` handling in `class-metadata-renderer.php`. # Extract the relevant lines from `class-metadata-renderer.php` to review the handling of `custom_metadata`. rg --type php --context 10 'custom_metadata' src/UI/class-metadata-renderer.phpLength of output: 816
src/Metadata/class-metadata-builder.php (1)
123-136
: The implementation ofbuild_post_id
inMetadata_Builder
is secure and efficient.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
Review details
Configuration used: .coderabbit.yaml
Review profile: CHILL
Files selected for processing (1)
- tests/Integration/Endpoints/RestMetadataTest.php (2 hunks)
Additional context used
Path-based instructions (1)
tests/Integration/Endpoints/RestMetadataTest.php (1)
Pattern
**/*.{html,php}
: "Perform a detailed review of the provided code with following key aspects in mind:
- Review the HTML and PHP code to ensure it is well-structured and adheres to best practices.
- Ensure the code follows WordPress coding standards and is well-documented.
- Confirm the code is secure and free from vulnerabilities.
- Optimize the code for performance, removing any unnecessary elements.
- Validate comments for accuracy, currency, and adherence to WordPress coding standards.
- Ensure each line comment concludes with a period.
- Verify code compatibility with the latest version of WordPress, avoiding deprecated functions or features."
Additional comments not posted (1)
tests/Integration/Endpoints/RestMetadataTest.php (1)
475-476
: Correct implementation of repeated meta tags.The test method
test_get_rendered_repeated_metas
correctly checks the inclusion of additional metadata in the meta tags, ensuring that the custom metadata is properly formatted and included.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
Review details
Configuration used: .coderabbit.yaml
Review profile: CHILL
Files selected for processing (1)
- tests/Integration/Endpoints/RestMetadataTest.php (2 hunks)
Files skipped from review as they are similar to previous changes (1)
- tests/Integration/Endpoints/RestMetadataTest.php
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for working on this! Left a suggestion and a question 🙂
@@ -404,7 +404,8 @@ public function test_get_rendered_meta_json_ld(): void { | |||
$this->go_to( (string) $this->get_permalink( $post_id ) ); | |||
|
|||
$meta_string = self::$rest->get_rendered_meta( 'json_ld' ); | |||
$expected = '<script type="application/ld+json">{"@context":"https:\/\/schema.org","@type":"NewsArticle","headline":"My test_get_rendered_meta_json_ld title","url":"http:\/\/example.org\/?p=' . $post_id . '","mainEntityOfPage":{"@type":"WebPage","@id":"http:\/\/example.org\/?p=' . $post_id . '"},"thumbnailUrl":"","image":{"@type":"ImageObject","url":""},"articleSection":"Uncategorized","author":[],"creator":[],"publisher":{"@type":"Organization","name":"Test Blog","logo":""},"keywords":[],"dateCreated":"' . $date . '","datePublished":"' . $date . '","dateModified":"' . $date . '"}</script>'; | |||
$expected = '<script type="application/ld+json">{"@context":"https:\/\/schema.org","@type":"NewsArticle","headline":"My test_get_rendered_meta_json_ld title","url":"http:\/\/example.org\/?p=' . $post_id . '","mainEntityOfPage":{"@type":"WebPage","@id":"http:\/\/example.org\/?p=' . $post_id . '"},"thumbnailUrl":"","image":{"@type":"ImageObject","url":""},"articleSection":"Uncategorized","author":[],"creator":[],"publisher":{"@type":"Organization","name":"Test Blog","logo":""},"keywords":[],"dateCreated":"' . $date . '","datePublished":"' . $date . '","dateModified":"' . $date . '","custom_metadata":"{\\"postID\\":' . $post_id . '}"}</script>' . |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need the \\
in {\\"postID\\"
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Most definitely, even the docs call for it:
Escape double quotes in JSON item values. Double quotes should be escaped with a backslash symbol like this:
\"
.
And then they provide clearly broken JSON in the example 🤷
https://docs.parse.ly/api-custom-metadata/#h-json-ld-configuration
Co-authored-by: Alex Cicovic <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
Review details
Configuration used: .coderabbit.yaml
Review profile: CHILL
Files selected for processing (2)
- src/Metadata/class-metadata-builder.php (1 hunks)
- tests/Integration/Endpoints/RestMetadataTest.php (2 hunks)
Files skipped from review as they are similar to previous changes (2)
- src/Metadata/class-metadata-builder.php
- tests/Integration/Endpoints/RestMetadataTest.php
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Outside diff range and nitpick comments (1)
src/Metadata/class-metadata-builder.php (1)
122-135
: Consider adding error handling and sanitization.The implementation looks good and follows WordPress coding standards. However, consider these improvements:
- Add error handling for
wp_json_encode
failure- Sanitize the post ID before encoding
Apply this diff to improve error handling and sanitization:
protected function build_post_id( WP_Post $post ): void { + $post_id = absint( $post->ID ); + $json_data = wp_json_encode( + array( + 'wpParselyPostID' => $post_id, + ) + ); + + if ( false !== $json_data ) { + $this->metadata['custom_metadata'] = $json_data; + } - $this->metadata['custom_metadata'] = wp_json_encode( - array( - 'wpParselyPostID' => $post->ID, - ) - ); }
📜 Review details
Configuration used: .coderabbit.yaml
Review profile: CHILL
📒 Files selected for processing (2)
src/Metadata/class-metadata-builder.php
(1 hunks)tests/Integration/Endpoints/RestMetadataTest.php
(2 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
src/Metadata/class-metadata-builder.php (1)
Pattern **/*.{html,php}
: "Perform a detailed review of the provided code with following key aspects in mind:
- Review the HTML and PHP code to ensure it is well-structured and adheres to best practices.
- Ensure the code follows WordPress coding standards and is well-documented.
- Confirm the code is secure and free from vulnerabilities.
- Optimize the code for performance, removing any unnecessary elements.
- Validate comments for accuracy, currency, and adherence to WordPress coding standards.
- Ensure each line comment concludes with a period.
- Verify code compatibility with the latest version of WordPress, avoiding deprecated functions or features."
tests/Integration/Endpoints/RestMetadataTest.php (1)
Pattern **/*.{html,php}
: "Perform a detailed review of the provided code with following key aspects in mind:
- Review the HTML and PHP code to ensure it is well-structured and adheres to best practices.
- Ensure the code follows WordPress coding standards and is well-documented.
- Confirm the code is secure and free from vulnerabilities.
- Optimize the code for performance, removing any unnecessary elements.
- Validate comments for accuracy, currency, and adherence to WordPress coding standards.
- Ensure each line comment concludes with a period.
- Verify code compatibility with the latest version of WordPress, avoiding deprecated functions or features."
🔇 Additional comments (2)
tests/Integration/Endpoints/RestMetadataTest.php (2)
406-407
: LGTM! The test correctly verifies the custom metadata implementation.
The test case properly validates that:
- The post ID is correctly encoded in the JSON-LD output
- The post ID is properly escaped in the meta tag
475-476
: LGTM! The test case validates repeated meta tags.
The test correctly verifies that:
- The section meta tag is present
- The post ID is properly encoded in the parsely-metadata meta tag
Description
Let's add a post ID to the custom metadata.
https://docs.parse.ly/api-custom-metadata/
CC @medeiros-amanda @rebeccahum @robersongomes
Motivation and context
Injecting post IDs will let Parsely link back to the edit form of the post.
As compared to LD-JSON (#2510), we can expect that there are no SEO implications.
How has this been tested?
LD-JSON works as usual.
Screenshots (if appropriate)
Summary by CodeRabbit
New Features
Bug Fixes
Tests